namespace NVelocity.Test {
    /*
	* The Apache Software License, Version 1.1
	*
	* Copyright (c) 2001 The Apache Software Foundation.  All rights
	* reserved.
	*
	* Redistribution and use in source and binary forms, with or without
	* modification, are permitted provided that the following conditions
	* are met:
	*
	* 1. Redistributions of source code must retain the above copyright
	*    notice, this list of conditions and the following disclaimer.
	*
	* 2. Redistributions in binary form must reproduce the above copyright
	*    notice, this list of conditions and the following disclaimer in
	*    the documentation and/or other materials provided with the
	*    distribution.
	*
	* 3. The end-user documentation included with the redistribution, if
	*    any, must include the following acknowlegement:
	*       "This product includes software developed by the
	*        Apache Software Foundation (http://www.apache.org/)."
	*    Alternately, this acknowlegement may appear in the software itself,
	*    if and wherever such third-party acknowlegements normally appear.
	*
	* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
	*    Foundation" must not be used to endorse or promote products derived
	*    from this software without prior written permission. For written
	*    permission, please contact apache@apache.org.
	*
	* 5. Products derived from this software may not be called "Apache"
	*    nor may "Apache" appear in their names without prior written
	*    permission of the Apache Group.
	*
	* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
	* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
	* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
	* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
	* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
	* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
	* SUCH DAMAGE.
	* ====================================================================
	*
	* This software consists of voluntary contributions made by many
	* individuals on behalf of the Apache Software Foundation.  For more
	* information on the Apache Software Foundation, please see
	* <http://www.apache.org/>.
	*/
    using System;
    using System.Collections;
    using Commons.Collections;
	
    /// <summary> Tests for the Commons ExtendedProperties class. This is an identical
    /// copy of the ConfigurationTestCase, which will disappear when
    /// the Configuration class does
    /// *
    /// </summary>
    /// <author> <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    /// </author>
    /// <author> <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
    /// </author>
    /// <version> $Id: CommonsExtPropTestCase.cs,v 1.2 2002/07/30 07:26:51 corts Exp $
    /// 
    /// </version>
    public class CommonsExtPropTestCase:BaseTestCase {
	/// <summary> Comparison directory.
	/// </summary>
	private const System.String COMPARE_DIR = "../test/configuration/compare";
		
	/// <summary> Results directory.
	/// </summary>
	private const System.String RESULTS_DIR = "../test/configuration/results";
		
	/// <summary> Test configuration
	/// </summary>
	private const System.String TEST_CONFIG = "../test/configuration/test.config";
		
	/// <summary> Creates a new instance.
	/// </summary>
	public CommonsExtPropTestCase():base("CommonsExtPropTestCase") {
	}

	public CommonsExtPropTestCase(String name):base(name) {
	}
		
		
	/// <summary> Runs the test.
	/// </summary>
	public virtual void  Test_run() {
	    System.IO.StreamWriter result = null;
	    ExtendedProperties c = null;
	    try {
		assureResultsDirectoryExists(RESULTS_DIR);
		c = new ExtendedProperties(TEST_CONFIG);
		result = new System.IO.StreamWriter(getFileName(RESULTS_DIR, "output", "res"));
	    } catch (System.Exception e) {
		throw new Exception("Cannot setup CommonsExtPropTestCase!", e);
	    }
				
	    message(result, "Testing order of keys ...");
	    showIterator(result, c.Keys);
				
	    message(result, "Testing retrieval of CSV values ...");
	    showVector(result, c.GetVector("resource.loader"));
				
	    message(result, "Testing subset(prefix).getKeys() ...");
	    ExtendedProperties subset = c.Subset("file.resource.loader");
	    showIterator(result, subset.Keys);
				
	    message(result, "Testing getVector(prefix) ...");
	    showVector(result, subset.GetVector("path"));
				
	    message(result, "Testing getString(key) ...");
	    result.Write(c.GetString("config.string.value"));
	    result.Write("\n\n");
				
	    message(result, "Testing getBoolean(key) ...");
	    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Boolean.toString' may return a different value. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1043"'
	    result.Write(c.GetBoolean("config.boolean.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getByte(key) ...");
	    result.Write(c.GetByte("config.byte.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getShort(key) ...");
	    result.Write(c.GetShort("config.short.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getInt(key) ...");
	    result.Write(c.GetInt("config.int.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getLong(key) ...");
	    result.Write(c.GetLong("config.long.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getFloat(key) ...");
	    result.Write(c.GetFloat("config.float.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing getDouble(key) ...");
	    result.Write(c.GetDouble("config.double.value").ToString());
	    result.Write("\n\n");
				
	    message(result, "Testing escaped-comma scalar...");
	    result.Write(c.GetString("escape.comma1"));
	    result.Write("\n\n");
				
	    message(result, "Testing escaped-comma vector...");
	    showVector(result, c.GetVector("escape.comma2"));
	    result.Write("\n\n");
				
	    result.Flush();
	    result.Close();
				
	    if (!isMatch(RESULTS_DIR, COMPARE_DIR, "output", "res", "cmp")) {
		Fail("Output incorrect.");
	    }
	}
		
	private void  showIterator(System.IO.StreamWriter result, IEnumerator i) {
	    while (i.MoveNext()) {
		result.Write((System.String) i.Current);
		result.Write("\n");
	    }
	    result.Write("\n");
	}
		
	private void  showVector(System.IO.StreamWriter result, System.Collections.ArrayList v) {
	    for (int j = 0; j < v.Count; j++) {
		result.WriteLine((System.String) v[j]);
	    }
	    result.Write("\n");
	}
		
	private void  message(System.IO.StreamWriter result, System.String message) {
	    result.WriteLine("--------------------------------------------------");
	    result.WriteLine(message);
	    result.WriteLine("--------------------------------------------------");
	    result.WriteLine("");
	}
    }
}